import stravel.*
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

class BootStrap {

	def init = { servletContext -> initValue() }


	def initValue(){

		def userRole = Role.findByAuthority('ROLE_USER') ?: new Role(authority: 'ROLE_USER').save(failOnError: true)
		def adminRole = Role.findByAuthority('ROLE_ADMIN') ?: new Role(authority: 'ROLE_ADMIN').save(failOnError: true)
		
		if(Location.count() == 0){
			initData2()
		}
	}

	void initData2(){
		//Country country = new Country(name:'MALAYSIA').save(flush:true)
		String url = "";
		url = "http://www.hotels.org.my/ordinary-member.html?letter=A&start=0";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=A&start=10";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=A&start=20";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=A&start=30";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=A&start=40";
		exec(url);
		
		url = "http://www.hotels.org.my/ordinary-member.html?letter=B&start=0";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=B&start=10";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=B&start=20";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=B&start=30";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=B&start=40";
		exec(url);
		
		url = "http://www.hotels.org.my/ordinary-member.html?letter=C&start=0";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=C&start=10";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=C&start=20";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=C&start=30";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=C&start=40";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=C&start=50";
		exec(url);
		
		String letter = "";
		letter = "D"
		url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=0";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=10";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=20";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=30";
		exec(url);
		
		letter = "E"
		url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=0";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=10";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=20";
		exec(url);
		
		letter = "F"
		url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=0";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=10";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=20";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=30";
		exec(url);
		
		letter = "G"
		url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=0";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=10";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=20";
		exec(url);
		url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=30";
		exec(url);
		
		
		letter = "H"
		for(int i=0;i<6;i++){
			url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=${i*10}";
			exec(url);
		}
		
		letter = "I"
		for(int i=0;i<3;i++){
			url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=${i*10}";
			exec(url);
		}
		
		letter = "J"
		for(int i=0;i<2;i++){
			url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=${i*10}";
			exec(url);
		}
		
		letter = "K"
		for(int i=0;i<3;i++){
			url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=${i*10}";
			exec(url);
		}
		
		letter = "L"
		for(int i=0;i<4;i++){
			url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=${i*10}";
			exec(url);
		}
		
		letter = "M"
		for(int i=0;i<5;i++){
			url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=${i*10}";
			exec(url);
		}
		
		
		letter = "N"
		for(int i=0;i<2;i++){
			url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=${i*10}";
			exec(url);
		}
		
		letter = "0"
		for(int i=0;i<2;i++){
			url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=${i*10}";
			exec(url);
		}
		
		letter = "P"
		for(int i=0;i<8;i++){
			url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=${i*10}";
			exec(url);
		}
		
		letter = "Q"
		for(int i=0;i<1;i++){
			url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=${i*10}";
			exec(url);
		}
		
		letter = "R"
		for(int i=0;i<5;i++){
			url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=${i*10}";
			exec(url);
		}
		
		letter = "S"
		for(int i=0;i<10;i++){
			url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=${i*10}";
			exec(url);
		}
		
		letter = "T"
		for(int i=0;i<4;i++){
			url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=${i*10}";
			exec(url);
		}
		
		letter = "U"
		for(int i=0;i<1;i++){
			url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=${i*10}";
			exec(url);
		}
		
		letter = "V"
		for(int i=0;i<1;i++){
			url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=${i*10}";
			exec(url);
		}
		
		
		letter = "W"
		for(int i=0;i<1;i++){
			url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=${i*10}";
			exec(url);
		}
		
		letter = "Y"
		for(int i=0;i<1;i++){
			url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=${i*10}";
			exec(url);
		}
		
		letter = "Z"
		for(int i=0;i<1;i++){
			url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=${i*10}";
			exec(url);
		}
		
		letter = "0-9"
		for(int i=0;i<1;i++){
			url = "http://www.hotels.org.my/ordinary-member.html?letter=${letter}&start=${i*10}";
			exec(url);
		}
		
		
		
		
	}

	void exec(String url){
		Document doc;
		try {


			// need http protocol
			doc = Jsoup.connect(url).get();

			// get page title
			String title = doc.title();
			println("title : " + title);
			Elements tables = doc.select("table");
			for (Element table : tables) {
				if(table.className()?.trim()=="sobi2Listing"){
					Elements tbodys = table.children();
					for (Element tbody : tbodys) {
						if ("tbody" == tbody.nodeName()?.toLowerCase()) {
							//System.out.println(tbody.nodeName());
						   extractTbody(tbody);
						}
					}
				}
			}


			//System.out.println(td.nodeName()+":"+td.className());

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private Country createCountry(){
		Country country = Country.findByName("MALAYSIA")
		if(!country){ 
		country = new Country(name:'MALAYSIA')
		country.save(flush:true)
		return country
		}else{
			return country
		}
	}
	
	private StarRating createStarRating(String name){
		if(!name){
			name = "UNKNOWN"
		}
		def state = StarRating.findByName(name)
		if(!state){
			state = new StarRating(name:name)
			state.save(flush:true)
			return state
		}else{
			return state
		}
	}
	
	private State createState(String name){
		if(!name){
			name = "UNKNOWN"
		}
		def state = State.findByName(name)
		if(!state){
			state = new State(name:name, parent:createCountry())
			state.save(flush:true)
			return state
		}else{
			return state
		}
	}
	
	
	private City createCity(String name, String stateName){
		def city = City.findByName(name)
		if(!city){
			city = new City(name:name, parent:createState(stateName))
			city.save(flush:true)
			return city
		}else{
			return city
		}
	}
	
	private void extractTbody(Element tbody) {
		Elements trs = tbody.children();
		for (Element tr : trs) {
			if ("tr"== tr.nodeName()?.toLowerCase()) {
				Elements tds = tr.children();
				for (Element td : tds) {
						//System.out.println(td.nodeName()+":"+td.className());
						Elements ps = td.select("p");
						String hname = ""
                        for (Element p : ps) {
                            Elements asx = p.select("a");
							for (Element a : asx) {
								println "a ${a.text()}"
								hname = a.text()
							}
                                    
                        }
						Elements tables = td.select("table")
						def mapF = [:]
						for (Element table : tables) {
							Elements tbodys = table.children();
							for (Element tbodyx : tbodys) {
								Elements trsx = tbodyx.children();
								for (Element trx : trsx) {
									Elements tdsx = trx.children();
									String[] tex = new String[2]
									int w = 0;
									for (Element tdx : tdsx) {
										tex[w] = tdx.text()
										w++
									}
									mapF.put(tex[0], tex[1])
									println "${tex[0]}:${tex[1]}"
									
								}
							}
						}
						City city = createCity(mapF.get("City / Province"), mapF.get("State"))
						println "OK1"
						StarRating starRating = createStarRating(mapF.get("Star Rating"))
						Hotel hotel = new Hotel()
						hotel.name = hname
						hotel.address1 = mapF.get("Address 1")
						hotel.address2 = mapF.get("Address 2")
						hotel.postcode = mapF.get("Postcode")
						hotel.generalLine = mapF.get("General Line")
						hotel.starRating = starRating
						hotel.state = city.parent
						hotel.city = city
						hotel.save(flush:true)

				}
				//trNode.
			}

		}
	}


	void initData(){
		Country country = new Country(name:'MALAYSIA').save(flush:true)

		State perlis = new State(parent:country,name:'PERLIS').save(flush:true)
		City kangar = new City(name:"KANGAR",parent:perlis).save(flush:true)

		State kedah = new State(parent:country,name:'KEDAH').save(flush:true)
		City asr = new City(name:"ALOR STAR",parent:kedah).save(flush:true)

		State penang = new State(parent:country,name:'PULAU PINANG').save(flush:true)
		City gt = new City(name:"GEORGETOWN",parent:penang).save(flush:true)

		State perak = new State(parent:country,name:'PERAK').save(flush:true)
		City ipoh = new City(name:"IPOH",parent:perak).save(flush:true)

		State selangor = new State(parent:country,name:'SELANGOR').save(flush:true)
		City sa = new City(name:"SHAH ALAM",parent:selangor).save(flush:true)

		State ns = new State(parent:country,name:'NEGRI SEMBILAN').save(flush:true)
		City seremban = new City(name:"SEREMBAN",parent:ns).save(flush:true)

		State mel = new State(parent:country,name:'MELAKA').save(flush:true)
		City melaka = new City(name:"BANDAR MELAKA",parent:mel).save(flush:true)

		State joh = new State(parent:country,name:'JOHOR').save(flush:true)
		City jb = new City(name:"JOHOR BAHRU",parent:joh).save(flush:true)

		State pah = new State(parent:country,name:'PAHANG').save(flush:true)
		City kuantan = new City(name:"KUANTAN",parent:pah).save(flush:true)

		State ter = new State(parent:country,name:'TERENGGANU').save(flush:true)
		City kt = new City(name:"KUALA TERANGGANU",parent:pah).save(flush:true)

		State kel = new State(parent:country,name:'KELANTAN').save(flush:true)
		City kb = new City(name:"KOTA BAHARU",parent:pah).save(flush:true)

		State sab = new State(parent:country,name:'SABAH').save(flush:true)
		City kk = new City(name:"KOTA KINABALU",parent:pah).save(flush:true)

		State sar = new State(parent:country,name:'SARAWAK').save(flush:true)
		City kuc = new City(name:"KUCHING",parent:sar).save(flush:true)

		State kl = new State(parent:country,name:'KUALA LUMPUR').save(flush:true)
		City kul = new City(name:"KUALA LUMPUR",parent:kl).save(flush:true)

		State put = new State(parent:country,name:'PUTRAJAYA').save(flush:true)
		City pt = new City(name:"PUTRAJAYA",parent:put).save(flush:true)
	}
	def destroy = {
	}
}
